package LW.org.action;

import LW.org.model.Room;
import LW.org.model.RoomUse;
import LW.org.service.roomService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("roomAction")
public class roomAction extends ActionSupport {

    @Autowired
    @Qualifier("roomServiceImple")
    private roomService roomservice;
    private HttpServletResponse response;
    private String name;
    private String idCard;
    private String tel;
    private int roomId;
    private int oldRoomid;
    private String inTime;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public int getOldRoomid() {
        return oldRoomid;
    }

    public void setOldRoomid(int oldRoomid) {
        this.oldRoomid = oldRoomid;
    }

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    public String getInTime() {
        return inTime;
    }

    public void setInTime(String inTime) {
        this.inTime = inTime;
    }

    public void initicalResponse() {
        response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.setHeader("Access-Control-Allow-Origin", "*");
    }

    public void showAllroomInfo() throws Exception {
        try {
            initicalResponse();
            List<Room> all = roomservice.find_all();
            if (all != null) {
                JSONArray json = new JSONArray().fromObject(all);
                String infoChunk = json + "";
                response.getWriter().write(infoChunk);
            }
        } catch (Exception e) {
            System.out.println("showAllroomInfo has error : " + e.toString());
        }
    }

    public void bookRoom() throws Exception {
        initicalResponse();
        RoomUse newone = new RoomUse();
        newone.setHotelClientName(this.name);
        newone.setHotelClientIdcard(this.idCard);
        newone.setHotelClientTel(this.tel);
        newone.setHotelInTime(new Date());
        Boolean status = roomservice.bookRoom(newone, this.roomId);
        if (status) {
            response.getWriter().write("GOOD");
        } else {
            response.getWriter().write("FAILED");
        }
    }

    public void checkOut() throws Exception {
        initicalResponse();
        Boolean status = roomservice.checkOut(this.roomId, false);
        if (status) {
            response.getWriter().write("GOOD");
        } else {
            response.getWriter().write("FAILED");
        }
    }

    @SuppressWarnings("unchecked")
    public void find_bookRoom() throws Exception {
        initicalResponse();
        RoomUse one = roomservice.find_bookRoom(this.roomId, false);
        if (one != null) {
            System.out.println(one.getHotelClientName());
            Map map = new HashMap();
            map.put("hotelInfoId", one.getHotelInfoId());
            map.put("hotelClientName", one.getHotelClientName());
            map.put("hotelClientIdcard", one.getHotelClientIdcard());
            map.put("hotelClientTel", one.getHotelClientTel());
            map.put("roomId", one.getRoom().getRoomId());
            map.put("hotelInTime", one.getHotelInTime().toString());
            JSONArray json = new JSONArray().fromObject(map);
            response.getWriter().write(json + "");
        }
    }

    @SuppressWarnings("unchecked")
    public void selectInroom() throws Exception {
        initicalResponse();
        int index = 0;
        Map allInfo = new HashedMap();
        List<RoomUse> all = roomservice.return_all_roomUse();
        if (all == null) {
            response.getWriter().write("N");
        } else if (all != null) {
            for (RoomUse single : all) {
                if (!single.getHotelOutflag()) {
                    Map map = new HashMap();
                    map.put("hotelInfoId", single.getHotelInfoId());
                    map.put("hotelClientName", single.getHotelClientName());
                    map.put("hotelClientIdcard", single.getHotelClientIdcard());
                    map.put("hotelClientTel", single.getHotelClientTel());
                    map.put("roomId", single.getRoom().getRoomId());
                    map.put("hotelInTime", single.getHotelInTime().toString());
                    System.out.println(single.toString());
                    allInfo.put(index + "", map);
                    index++;
                }
            }
            JSONArray json = new JSONArray().fromObject(allInfo);
            response.getWriter().write(json + "");
        }
    }

    public void updatePeopleinfo() throws Exception {
        try {

        } catch (Exception e) {

        }
    }

}
